﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace OrangePrj
{
    public partial class ChangePasswordForm : Form
    {
        private string name, password;

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                this.password = value;
            }
        }
        public ChangePasswordForm(User user)
        {
            InitializeComponent();
            this.label1.Text = user.Name.ToUpper();
            this.name = user.Name.ToLower();
            this.password = user.Password.ToLower();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            int bytesRead;
            const int bufSize = 1024;

            string text1, text2, text3;
            string newPassword = tbNewPassword.Text.ToLower();
            string oldPassword = tbOldPassword.Text.ToLower();
            string confirmPassword = tbConfirmPaaword.Text.ToLower();

            try
            {
                string path = Directory.GetCurrentDirectory();

                //Расшифровка

                Stream input = File.OpenRead(path + @"\Users");
                Stream outputStream = File.OpenWrite(path + @"\Test1");

                byte[] buffer = new byte[bufSize];
                while ((bytesRead = input.Read(buffer, 0, bufSize)) > 0)
                {
                    for (int i = 0; i < bufSize; i++)
                        buffer[i] -= 20;

                    outputStream.Write(buffer, 0, bytesRead);
                }
                input.Close();
                outputStream.Close();

                if (oldPassword.CompareTo(this.password) == 0)
                {
                    if (newPassword != "")
                    {
                        if (newPassword.CompareTo(confirmPassword) == 0)
                        {
                            FileInfo inputFile = new FileInfo(path + @"\Test1");
                            StreamReader inputStream = inputFile.OpenText();
                            StreamWriter outputS = new StreamWriter(path + @"\Te", false);
                            while ((text1 = inputStream.ReadLine()) != null &&
                                (text2 = inputStream.ReadLine()) != null &&
                                (text3 = inputStream.ReadLine()) != null)
                            {
                                //изменение пароля
                                outputS.WriteLine(text1);
                                if (text1.ToLower().CompareTo(this.name) == 0)
                                    outputS.WriteLine(newPassword);
                                else
                                    outputS.WriteLine(text2);

                                outputS.WriteLine(text3);
                                
                            }

                            inputStream.Close();
                            outputS.Close();
                            inputFile.Delete();
                            inputFile = new FileInfo(path + @"\Te");
                            inputFile.MoveTo(inputFile.FullName + "st1");
                            Close();
                        }
                        else
                        {
                            label5.Text = "Введите одинаковый пароль!";
                            label5.Show();
                            Height = 279;
                        }
                    }
                    else
                    {
                        label5.Show();
                        label5.Text = "Нельзя установить пустой пароль!";
                        Height = 279;
                    }

                }
                else
                {
                    label5.Show();
                    label5.Text = "Неправильный старый пароль!";
                    Height = 279;
                }

                input = File.OpenRead(path + @"\Test1");
                outputStream = File.OpenWrite(path + @"\Users");

                while ((bytesRead = input.Read(buffer, 0, bufSize)) > 0)
                {
                    for (int i = 0; i < bufSize; i++)
                        buffer[i] += 20;

                    outputStream.Write(buffer, 0, bytesRead);
                }
                input.Close();
                outputStream.Close();
                FileSystemInfo temp = new FileInfo(path + @"\Test1");
                temp.Delete();
                
            }
            catch(FileNotFoundException){
                label5.Show();
                label5.Text = "Файл не найден! Обратитесь к Администратору!";
                Height = 279;
            }
            catch(Exception  ex){
                MessageBox.Show("Ошибка: " + ex.Message);
            }
            
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
